/* Copyright (c) 2010 EPiServer AB
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
 * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
 * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
using EPiServer.Core;
using EPiServer.SpecializedProperties;

namespace EPiServer.Municipality.Extensions {
    /// <summary>
    /// 
    /// </summary>
    /// <remarks></remarks>
    /// <example></example>
    public static class LinkItemCollectionExtensions {
        /* *******************************************************************
		*  Properties
		* *******************************************************************/
        /* *******************************************************************
		*  Constructors
		* *******************************************************************/
        /* *******************************************************************
		*  Methods
		* *******************************************************************/
        #region public static PageDataCollection ToPageDataCollection(this LinkItemCollection linkItemCollection)
        /// Get a PageDataCollection with all the EPiServer pages in a LinkItemCollection
        /// </summary>
        /// <param name="linkItemCollection"></param>
        /// <returns>All the EPiServer pages in a LinkItemCollection</returns>
        public static PageDataCollection ToPageDataCollection(this LinkItemCollection linkItemCollection) {
            return ToPageDataCollection(linkItemCollection, 0);
        }
        #endregion
        #region public static PageDataCollection ToPageDataCollection(this LinkItemCollection linkItemCollection, int pageTypeId)
        /// Get a PageDataCollection with all the EPiServer pages in a LinkItemCollection
        /// </summary>
        /// <param name="linkItemCollection"></param>
        /// <param name="pageTypeId"></param>
        /// <returns>All the EPiServer pages in a LinkItemCollection</returns>
        public static PageDataCollection ToPageDataCollection(this LinkItemCollection linkItemCollection, int pageTypeId) {
            var pageDataCollection = new PageDataCollection();
            foreach (var linkItem in linkItemCollection) {
               string mappedHref = linkItem.GetMappedHref();
                PageReference pageRef = PageReference.ParseUrl(mappedHref);
                if (!PageReference.IsNullOrEmpty(pageRef)) {
                    var page = DataFactory.Instance.GetPage(pageRef);

                    if (page != null) {
                        if (pageTypeId == 0)
                            pageDataCollection.Add(page);
                        else if (page.PageTypeID == pageTypeId)
                            pageDataCollection.Add(page);
                    }
                }
            }

            return pageDataCollection;
        }
        #endregion
        /* *******************************************************************
		*  Event methods
		* *******************************************************************/
        
    }
}